// pages/myForecast/myForecast.js
const db = wx.cloud.database();
const forecastJoin_table=db.collection('forecastJoin_table');
const forecast_table = db.collection('forecast_table');
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
        
    forecast:[],

  },


navigate(e){

wx.navigateTo({
  url: '../joinForecast/jornForecast?id='+e.currentTarget.dataset.id,
})

},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
this.getJoinforecast().then(this.getForecast)
  },

  getJoinforecast(e){
    var p=  new Promise((resolve,reject)=>{
    forecastJoin_table.where({
 
      userId:app.globalData.openid
    }).get({
      success:(res)=>{
        console.log(res.data)
    
        this.setData({
          forecast:[...res.data],  
        })
        console.log(this.data.forecast)
        console.log('拿forecastJoin表数据 完成')
        resolve()
      },
      fail(err) {
     console.log('取数据失败')
     resolve()
      }
          
      })
    })
    return p

  },
//循环从预测表中读取对应信息
  getForecast(e){
    var p=  new Promise((resolve,reject)=>{
    for(let i=0;i<this.data.forecast.length;i++){
      console.log(this.data.forecast[i].forecastId)
      forecast_table.where({
       
        forecastId:this.data.forecast[i].forecastId
      }).get({
        success:(res)=>{
          console.log(res.data)
        this.setData({
          ['forecast['+i+'].title']:res.data[0].title,
          ['forecast['+i+'].type']:res.data[0].type,
          ['forecast['+i+'].examine']:res.data[0].examine,

        })
        resolve()
          console.log(this.data.forecast)
          console.log('拿forecast表数据 完成')
         
        },
        fail(err) {
       console.log('取数据失败')
       resolve()
        }
            
        })


    }

    })
    return p

  },
      //到底了再拿一次数据 一次拿20条
      getAgain() {
        console.log('拿forecast表数据 2')
      
          //先拿forecast表数据 1
    
          var p=  new Promise((resolve,reject)=>{
            forecastJoin_table.skip(this.data.forecast.length).where({
         
              userId:app.globalData.openid
            }).get({
              success:(res)=>{
                console.log(res.data)
            let arr=this.data.forecast
            arr.push(...res.data)
                this.setData({
                  forecast:arr,  
                })
                console.log(this.data.forecast)
                console.log('拿forecastJoin表数据 完成')
                resolve()
              },
              fail(err) {
             console.log('取数据失败')
             resolve()
              }
                  
              })
            })
            return p
     
    
      },

        /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
this.getAgain().then(this.getForecast)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})